/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
/**
 * @author Simon Oliver
 * @version 1.0
 */
 
package derek.util.drawing
{
	import derek.geom.rect;
	import flash.display.Graphics;
	import derek.util.drawing.styles.*;
	
	public class drawingUtility
	{
		public static function drawRect(graphics:Graphics,tRect:rect,tLineStyle:lineStyleFormat =null,tFillStyle:fillStyleFormat = null ):void
		{
			if (tLineStyle) tLineStyle.apply(graphics);
			else graphics.lineStyle(undefined);
			
			if (!tFillStyle) tFillStyle=fillStyleFormat.defaultStyle;
			tFillStyle.apply(graphics);
		
			if (tRect.roundedSize)
			{		
				executeDrawRoundedRect(graphics,tRect,tRect.roundedSize);
			}
			else
			{			
				executeDrawRect(graphics,tRect);
			}
			graphics.endFill();
		}
		
		public static function executeDrawRect(graphics:Graphics,tRect:rect):void
		{
			graphics.moveTo(tRect.left,tRect.top);		
			graphics.lineTo(tRect.right,tRect.top);
			graphics.lineTo(tRect.right,tRect.bottom);
			graphics.lineTo(tRect.left,tRect.bottom);
			graphics.lineTo(tRect.left,tRect.top);
		}
		
		public static function executeDrawRoundedRect(graphics:Graphics,tRect:rect,tRoundedness:Number):void
		{
			graphics.moveTo(tRect.left+tRoundedness,tRect.top);
			graphics.lineTo(tRect.right-tRoundedness,tRect.top);
			graphics.curveTo(tRect.right,tRect.top,tRect.right,tRect.top+tRoundedness);
			graphics.lineTo(tRect.right,tRect.bottom-tRoundedness);
			graphics.curveTo(tRect.right,tRect.bottom,tRect.right-tRoundedness,tRect.bottom);
			graphics.lineTo(tRect.left+tRoundedness,tRect.bottom);
			graphics.curveTo(tRect.left,tRect.bottom,tRect.left,tRect.bottom-tRoundedness);
			graphics.lineTo(tRect.left,tRect.top+tRoundedness);
			graphics.curveTo(tRect.left,tRect.top,tRect.left+tRoundedness,tRect.top);
		}
	}
}